package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.ContactNote;
import com.kaishengit.entity.Employee;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.RowMapper;

public class ContactNoteDao {
	private DBHelp<ContactNote> db = new DBHelp<ContactNote>();

	public List<ContactNote> findById(String id) {
		final String sql = "SELECT tcn.id,tcn.content, tcn.createtime,employeeid,contactid,te.username FROM t_contactnote AS tcn INNER  JOIN t_employee AS te ON employeeid = te.id WHERE contactid =? order by tcn.createtime desc limit 0,3 ";
		return db.executeQueryForList(sql, new ContactNoteRowMapper(), id);
	}
	
	private class ContactNoteRowMapper implements RowMapper<ContactNote> {

		public ContactNote mapRow(ResultSet rs) throws SQLException {
			ContactNote cn = new ContactNote();
			cn.setContactid(rs.getString("contactid"));
			cn.setContent(rs.getString("content"));
			cn.setCreatetime(rs.getString("createtime"));
			cn.setEmployeeid(rs.getString("employeeid"));
			cn.setId(rs.getString("id"));
			
			Employee e = new Employee();
			e.setUsername(rs.getString("username"));
			e.setId(rs.getString("employeeid"));
			cn.setEmployee(e);
			return cn;
		}
		
	}

	public void saveNote(ContactNote cn) {
		final String sql = "INSERT INTO t_contactnote(id,content,createtime,employeeid,contactid) VALUES(?,?,?,?,?)";
		db.executeSQL(sql, cn.getId(),cn.getContent(),cn.getCreatetime(),cn.getEmployeeid(),cn.getContactid());
		
	}

	public List<ContactNote> findAllById(String contactid) {
		final String sql = "SELECT tcn.id,tcn.content, tcn.createtime,employeeid,contactid,te.username FROM t_contactnote AS tcn INNER  JOIN t_employee AS te ON employeeid = te.id WHERE contactid =? order by tcn.createtime desc";
		return db.executeQueryForList(sql, new ContactNoteRowMapper(), contactid);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
